﻿using System;
using System.Collections.Generic;
using System.Web;
using FriendlyURL.Web.Domain.Abstract;

namespace FriendlyURL.Web.Domain.Impl
{
    /// <summary>
    /// MessageHandler is used as a layer between the Log class and all other classes to determine which messages should be written to the logs.
    /// </summary>
    public class MessageHandler : IMessageHandler
    {
        /// <summary>
        /// Used to log an error to the application error log
        /// </summary>
        /// <param name="ex">Exception</param>
        public void HandleError(Exception ex)
        {
            // Add information about the error to the additionalInfo dictionary
            Dictionary<string, string> additionalInfo = new Dictionary<string, string>();
            additionalInfo.Add("", "");
            additionalInfo.Add("Message", ex.Message);
            additionalInfo.Add("StackTrace", ex.StackTrace);
            additionalInfo.Add("Source", ex.Source);
            additionalInfo.Add("HelpLink", ex.HelpLink);

            // Log error.
            Log.Write(ex.Message, GlobalProperties.LogErrorName, additionalInfo);
        }

        /// <summary>
        /// Overloaded: HandleMessage method is used as an abstraction layer between the Log class and all other classes.  HandleMessage will determine
        /// if a message should really be written to the logs.
        /// <remarks>
        /// This overload will pass a message type of "Information" and a value of false to the shouldOverride parameter
        /// </remarks>
        /// </summary>
        /// <param name="message"></param>
        public void HandleMessage(string message)
        {
            HandleMessage(message, GlobalProperties.LogInfoName, false);
        }

        /// <summary>
        /// Overloaded: HandleMessage method is used as an abstraction layer between the Log class and all other classes.  HandleMessage will determine
        /// if a message should really be written to the logs.
        /// <remarks>
        /// This overload will pass a value of false to the shouldOverride parameter
        /// </remarks>
        /// </summary>
        /// <param name="message"></param>
        /// <param name="type"></param>
        public void HandleMessage(string message, string type)
        {
            HandleMessage(message, type, false);
        }

        /// <summary>
        /// HandleMessage method is used as an abstraction layer between the Log class and all other classes.  HandleMessage will determine
        /// if a message should really be written to the logs.
        /// </summary>
        /// <param name="message">String message that needs to be logged</param>
        /// <param name="type">String severity type of the message; Use GlobalProperites for values</param>
        /// <param name="shouldOverride">Boolean indicating whether or not to override the web.config's debug property for this particular message only</param>
        public void HandleMessage(string message, string type, bool shouldOverride)
        {
            // If the web.config compilation debug property is true or if we are overriding all logging the debug property
            // log the message; otherwise do not log the message
            if (HttpContext.Current.IsDebuggingEnabled || shouldOverride)
            {
                Log.Write(message, type);
            }
        }
    }
}
